/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TelasPedido;

import DAO.ItemDAO;
import DAO.NotaFiscalDAO;
import DAO.PedidoDAO;
import Pojo.NotaFiscal;
import Pojo.Pedido;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


public class TelaPedidosFechados extends javax.swing.JPanel {

    PedidoDAO pedidoDao;
    NotaFiscalDAO notaFiscalDao;
    ItemDAO itemDao;
    public TelaPedidosFechados(NotaFiscalDAO notaFiscalDao,PedidoDAO pedidoDao,ItemDAO itemDao) {
        initComponents();
        this.pedidoDao = pedidoDao;
        this.notaFiscalDao = notaFiscalDao;
        this.itemDao = itemDao;
        ArrayList<NotaFiscal> lista = notaFiscalDao.getNotasFiscais();
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        if(lista.isEmpty()){
            JOptionPane.showMessageDialog(null,"Nenhum Pedido Fechado.");
        }
        for (NotaFiscal notaFiscal : lista) {
            String [] linha = new String[]{notaFiscal.getPedido().getCliente().getNome(),Integer.toString(notaFiscal.getPedido().getCodigo()),
            Integer.toString(notaFiscal.getPedido().getHash().size()),
            Float.toString(notaFiscal.getPedido().getPreco())};
            modelo.addRow(linha);
        }
        jTable1.setModel(modelo);
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cliente", "Cod.Pedido", "Qnt.Itens", "Valor Total"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Buscar");

        jButton2.setText("Abrir Nota Fiscal");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 349, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(46, 46, 46))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(33, 33, 33)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap(17, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        ArrayList<NotaFiscal> lista =  notaFiscalDao.getNotasFiscais();
        int linha1 = jTable1.getSelectedRow();
        Pedido pedido = null;
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        if(linha1<0 || linha1> modelo.getRowCount()){
            JOptionPane.showMessageDialog(this,"Clique no pedido");
        }
        else{
            for(int i=0;i<modelo.getRowCount();i++){
                pedido = lista.get(i).getPedido();
                String codigo = (String)modelo.getValueAt(linha1, 1);
                if(pedido.getCodigo()==Integer.valueOf(codigo)){
                        TelaListarItensDoPedido telaListarItensDoPedido = new TelaListarItensDoPedido(pedidoDao,pedido,itemDao,1);
                        telaListarItensDoPedido.setVisible(true);
                        break;
                }
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
